#include "StepWays.h"


StepWays::StepWays(int stepsSum, int stepWaysSum, int *pStepsEachArray)
{
	pWaysArray=0;
	if(stepsSum>=1)
	{
		m_stepsSum=stepsSum;
		pWaysArray=new long[m_stepsSum+1];
	}
	if(stepWaysSum>=1)
		m_stepWaySum=stepWaysSum;
	pStepWay=pStepsEachArray;
	InitialWaysArray();
}


StepWays::~StepWays(void)
{
	if(pWaysArray!=0)
		delete[] pWaysArray;
}

void StepWays::InitialWaysArray()
{
	for(int i=1;i<pStepWay[0];i++)
	{
		pWaysArray[i]=0;
	}
	//pStepWay[m_stepWaySum]=this->m_stepsSum;
	pWaysArray[pStepWay[0]]=1;

	for(int k=0; k<m_stepWaySum-1; k++)
	{
		for( int t=pStepWay[k]+1; t<=pStepWay[k+1]; t++)
		{
			pWaysArray[t]=0;
			for(int j=0; j<=k; j++)
			{
				pWaysArray[t]=pWaysArray[t]+pWaysArray[t-pStepWay[j]];
			}
			if(t==pStepWay[k+1])
				pWaysArray[t]=pWaysArray[t]+1;			
		}
	}
	for(int n=pStepWay[m_stepWaySum-1]+1;n<=m_stepsSum; n++)
	{
		pWaysArray[n]=0;
		for(int j=0;j<m_stepWaySum;j++)
			pWaysArray[n]=pWaysArray[n]+pWaysArray[n-pStepWay[j]];
	}
}
int StepWays::GetWaysSum(int n)
{
	if(n>0)
		return pWaysArray[n];
	else
		return  -1;
}
